<?php

class ModuleLayoult extends fvLayoult {

	private $_pageInstance = null;
	private $_domInstance = null;
	private $_REGEXP = "/\(?%([\w]+)\(?([0-9]*[wc]?)\)?%\)?(\?([^\?;]+))?(\?([^\?;]+))?;?/i";

	function __construct(){
		$currentUrl = fvRoute::getInstance()->getModuleName();
		$p = PageManager::getInstance()->getByPageUrl($currentUrl);
		if (is_object($p[0])) {
			$this->_pageInstance = $p[0];
		} elseif (is_object($p = PageManager::getInstance()->getOneByPageName('default'))) {
			$this->_pageInstance = $p;
		} else fvDispatcher::redirect(fvSite::$fvConfig->get('page_404', 0, 404));

		$this->_domInstance = new DOMDocument();

		if (!($this->_domInstance->loadXML($this->_pageInstance->getPageContent()))) {
			fvDispatcher::redirect(fvSite::$fvConfig->get('page_error', 0, 302));
		}

		$this->setTitle($this->parseMeta($this->_pageInstance->page_title));
		$this->setDescription($this->parseMeta($this->_pageInstance->page_description));
		$this->setKeywords($this->parseMeta($this->_pageInstance->page_keywords));
		if ($currentUrl == 'index'){
			parent::__construct("main-index.tpl");
		}
		else{
			parent::__construct("main-inner.tpl");
		}
	}

	function parseMeta ($meta_value) {
		$request = fvRequest::getInstance()->getRequest();
		preg_match_all($this->_REGEXP, $meta_value, $matches);
		for ($i=0; $i< count($matches[0]); $i++) {
			$replace_text = "";
			$tmp_match = explode('_',$matches[1][$i]);
			switch ($tmp_match[0]) {
				case 'StaticPages':
					$replace_text = "";
					$StaticPage = StaticPagesManager::getInstance()->getBytech_url($request["module"]);
					if (count($StaticPage)){
						$replace_text = $StaticPage[0]->$tmp_match[1];
					}
					break;
			}
			$meta_value = preg_replace("/\s?" . preg_quote($matches[0][$i]) . "\s|\s" . preg_quote($matches[0][$i]) . "\s?/i", ' ' . preg_replace('[\n\r]', ' ', strip_tags($replace_text)) . ' ', $meta_value);
		}
		$meta_value = preg_replace('/ +/',' ', $meta_value);
		return trim($meta_value);
	}

	function getPageContent() {

	}

	function getPageContentPart($contentPartName) {
		$xpth = new DOMXPath($this->_domInstance);
		$currentPart = $xpth->evaluate("/page/content_part[@name='$contentPartName']");
		if ($currentPart->length == 1) {
			$currentPart = $currentPart->item(0);
		}
		else{
			return '';
		}
		return $this->_parseNode($currentPart);
	}

	private function _parseNode($node) {
		$result = '';
		$childNodes = array();
		foreach ($node->childNodes as $childNode) {
			$childNodes[] = $childNode;
		}
		usort($childNodes, array($this, '__cmp'));
		switch ($node->nodeName) {
			case 'module':
				$moduleName = $node->getAttribute('name');
				$moduleView = $node->getAttribute('view');
				$moduleParams = unserialize($node->getAttribute('parameters'));
				$module = fvDispatcher::getInstance()->getModule($moduleName, "module");
				return $module->showModule($moduleView, $moduleParams, $node->getAttribute("id"));
			case 'current_module':
				return $this->getModuleResult();
				break;
			case 'vertical_layoult':
				$data = '';
				$data .= "<table style='width: 100%' cellspacing=0 cellpadding=0 valign=top>";
				$heights = explode(',', $node->getAttribute('size'));
				$spacer = $node->getAttribute('spacer');
				foreach ($childNodes as $key => $childNode) {
					$data .= "<tr" . (($heights[$key] != "*")?" height='{$heights[$key]}'":'') ."><td valign='top'>".$this->_parseNode($childNode)."</td></tr>";
					if ($spacer) {
						$data .= "<tr height='{$spacer}px'><td>&nbsp;</td></tr>";
					}
				}
				$data .= "</table>";
				return $data;
			case 'horisontal_layoult':
				$data = '';
				$data .= "<table style='width: 100%' cellspacing=0 cellpadding=0 valign=top><tr>";
				$widths = explode(',', $node->getAttribute('size'));
				$spacer = $node->getAttribute('spacer');
				foreach ($childNodes as $key => $childNode) {
					$data .= "<td valign='top' height='100%'" . (($widths[$key] != "*")?" width='{$widths[$key]}'":'') .">".$this->_parseNode($childNode)."</td>";
					if ($spacer && ($key < (count($childNodes) - 1))) {
						$data .= "<td width='{$spacer}px'>&nbsp;</td>";
					}
				}
				$data .= "</tr></table>";
				return $data;
			case 'content_part':
				foreach ($childNodes as $childNode) {
					$result .= $this->_parseNode($childNode);
				}
				break;
		}
		return $result;
	}

	private function __cmp($a, $b) {
		if ($a->getAttribute('order') == $b->getAttribute('order')) return 0;
		return ($a->getAttribute('order') < $b->getAttribute('order')) ? -1 : 1;
	}

}

?>
